package com.pur.zsms.systemsetting.datadictionary.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

@Service("dictionaryService")
public class DataDictionaryServiceImpl implements IDataDictionaryService {

	@Resource(name = "dataDictionaryDao")
	private IDataDictionaryDao dataDictionaryDao;

	public IDataDictionaryDao getdataDictionaryServiceImpl() {
		return dataDictionaryDao;
	}

	public void setdataDictionaryServiceImpl(IDataDictionaryDao dataDictionaryServiceImpl) {
		this.dataDictionaryDao = dataDictionaryServiceImpl;
	}

	public PaginationSupport findCode(PageController pageController, CodeVo codevo, String codeTypeId) throws GenericException {
		return dataDictionaryDao.findCodeList(pageController, codevo, codeTypeId);
	}

	public List<CodeType> findById(long codeTypeId) throws GenericException {
		return dataDictionaryDao.findById(codeTypeId);
	}

	public void addCodetype(CodeType c) throws GenericException {
		dataDictionaryDao.addCodetype(c);
	}

	public void deleteCode(String phCodeId) throws GenericException {
		dataDictionaryDao.deleteCode(phCodeId);

	}

	public void addCode(Code c) throws GenericException {
		dataDictionaryDao.addCode(c);
	}

	public List<CodeType> findAllCodeType() throws GenericException {
		return dataDictionaryDao.findCodeTypeList();
	}

	@Override
	public String findCodeName(CodeVo codeVo) {
		return dataDictionaryDao.findCodeName(codeVo);
	}

	@Override
	public String getMaterType() throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList("mater_type");
		StringBuffer jsonMaterTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonMaterTypeString.append("[");
			for (Code code : codeList) {
				jsonMaterTypeString.append("{\"");
				jsonMaterTypeString.append("materType");
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getDescrib());
				jsonMaterTypeString.append("\",\"codeOder\":\"");
				jsonMaterTypeString.append(code.getCodeOder());
				jsonMaterTypeString.append("\"},");
			}
			jsonMaterTypeString = new StringBuffer(jsonMaterTypeString.substring(0, jsonMaterTypeString.length() - 1));
			jsonMaterTypeString.append("]");
		}
		return jsonMaterTypeString.toString();
	}

	public String getCodeJsonByCodeType(String codetype) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList(codetype);
		StringBuffer jsonCodeTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonCodeTypeString.append("[");
			for (Code code : codeList) {
				jsonCodeTypeString.append("{\"");
				jsonCodeTypeString.append("codeId");
				jsonCodeTypeString.append("\":\"");
				jsonCodeTypeString.append(code.getCodeId());
				jsonCodeTypeString.append("\",\"codeName\":\"");
				jsonCodeTypeString.append(code.getCodeName());
				jsonCodeTypeString.append("\"},");
			}
			jsonCodeTypeString = new StringBuffer(jsonCodeTypeString.substring(0, jsonCodeTypeString.length() - 1));
			jsonCodeTypeString.append("]");
		}
		return jsonCodeTypeString.toString();
	}

	public String getTurnoverType() throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList("turnoverType");
		StringBuffer jsonTurnoverTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonTurnoverTypeString.append("[");
			for (Code code : codeList) {
				jsonTurnoverTypeString.append("{\"");
				jsonTurnoverTypeString.append("turnoverType");
				jsonTurnoverTypeString.append("\":\"");
				jsonTurnoverTypeString.append(code.getCodeName());
				jsonTurnoverTypeString.append("\",\"codeId\":\"");
				jsonTurnoverTypeString.append(code.getCodeId());
				jsonTurnoverTypeString.append("\"},");
			}
			jsonTurnoverTypeString = new StringBuffer(jsonTurnoverTypeString.substring(0, jsonTurnoverTypeString.length() - 1));
			jsonTurnoverTypeString.append("]");
		}
		return jsonTurnoverTypeString.toString();
	}

	public String getCostType() throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList("costType");
		StringBuffer jsonTurnoverTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonTurnoverTypeString.append("[");
			for (Code code : codeList) {
				jsonTurnoverTypeString.append("{\"");
				jsonTurnoverTypeString.append("costType");
				jsonTurnoverTypeString.append("\":\"");
				jsonTurnoverTypeString.append(code.getCodeName());
				jsonTurnoverTypeString.append("\",\"codeId\":\"");
				jsonTurnoverTypeString.append(code.getCodeId());
				jsonTurnoverTypeString.append("\"},");
			}
			jsonTurnoverTypeString = new StringBuffer(jsonTurnoverTypeString.substring(0, jsonTurnoverTypeString.length() - 1));
			jsonTurnoverTypeString.append("]");
		}
		return jsonTurnoverTypeString.toString();
	}

	@Override
	public String pullDownList(String codeType, String codeId, String codeName) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList(codeType);
		StringBuffer jsonMaterTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonMaterTypeString.append("[");
			for (Code code : codeList) {
				jsonMaterTypeString.append("{\"");
				jsonMaterTypeString.append(codeId);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getCodeId());
				jsonMaterTypeString.append("\",\"");
				jsonMaterTypeString.append(codeName);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getCodeName());
				jsonMaterTypeString.append("\"},");
			}
			jsonMaterTypeString = new StringBuffer(jsonMaterTypeString.substring(0, jsonMaterTypeString.length() - 1));
			jsonMaterTypeString.append("]");
		}
		return jsonMaterTypeString.toString();
	}

	@Override
	public String pullDownListDiy(String codeType, String codeId, String codeName, String diy) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findCodeIdList(codeType);
		StringBuffer jsonMaterTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonMaterTypeString.append("[");
			if (diy.equals("1")) {
				jsonMaterTypeString.append("{\"");
				jsonMaterTypeString.append(codeId);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(0);
				jsonMaterTypeString.append("\",\"");
				jsonMaterTypeString.append(codeName);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append("供货商");
				jsonMaterTypeString.append("\"},");
			}
			for (Code code : codeList) {
				jsonMaterTypeString.append("{\"");
				jsonMaterTypeString.append(codeId);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getCodeId());
				jsonMaterTypeString.append("\",\"");
				jsonMaterTypeString.append(codeName);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getCodeName());
				jsonMaterTypeString.append("\"},");
			}
			jsonMaterTypeString = new StringBuffer(jsonMaterTypeString.substring(0, jsonMaterTypeString.length() - 1));
			jsonMaterTypeString.append("]");
		}
		return jsonMaterTypeString.toString();
	}

	@Override
	public String getMaterTypeName(int materType) throws GenericException {
		List<Code> codeList = dataDictionaryDao.findMaterTypeName(materType);
		return codeList.get(0).getDescrib();
	}

	public List<Code> getCodeList(String codeType) throws GenericException {
		return dataDictionaryDao.findCodeIdList(codeType);
	}

}
